<?php
class ICalendar
{
	private $eventResultSet;
	private $showLocations = true;
	private $showInvolvees = true;
	private $showComment = true;
	
	public function setEventsResultSet($resultSet)
	{
		$this->eventsResultSet = $resultSet;
	}
	
	public function showLocations($showlocations){$this->showLocations=$showlocations;}
	public function showInvolvees($showinvolvees){$this->showInvolvees=$showinvolvees;}
	public function showComment($showcomment){$this->showComment=$showcomment;}
	
	public function getCalendar()
	{
		header('Content-type: text/calendar; charset=utf-8');
		header("Content-Disposition: inline; filename=snap.ics");
		
		echo "BEGIN:VCALENDAR\r\n";
		echo "VERSION:2.0\r\n";
		echo "METHOD:PUBLISH\r\n";
		echo "PRODID:SNAP\r\n";
		
		echo "BEGIN:VTIMEZONE\r\n";
		echo "TZID:Europe/London\r\n";
		echo "BEGIN:DAYLIGHT\r\n";
		echo "TZOFFSETFROM:+0000\r\n";
		echo "TZOFFSETTO:+0100\r\n";
		echo "DTSTART:19810329T010000\r\n";
		echo "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\n";
		echo "TZNAME:BSTr\r\n";
		echo "END:DAYLIGHT\r\n";
		echo "BEGIN:STANDARD\r\n";
		echo "TZOFFSETFROM:+0100\r\n";
		echo "TZOFFSETTO:+0000\r\n";
		echo "DTSTART:19961027T020000\r\n";
		echo "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\n";
		echo "TZNAME:GMT\r\n";
		echo "END:STANDARD\r\n";
		echo "END:VTIMEZONE\r\n";
		
		//print_r($events)
		
		foreach($this->eventsResultSet->getEvents() as $event)
		
		{
			echo "BEGIN:VEVENT\r\n";
			echo "CLASS:PUBLIC\r\n";
			echo "UID:" . md5(uniqid(mt_rand(), true)) . "@stnicholascathedralmusic.co.uk\r\n";
			echo "CREATED:" . $event->getCreatedWhen()->format('Ymd\THis') . "\r\n";
			echo "SUMMARY:" . $event->getTitle() . "\r\n";
			$description = $event->getDescription();
			if($this->showComment)
				$description.= " - ".$event->getComment();
			if($this->showInvolvees)
				$description .= " [".$event->getInvolveesList()."]";
			if($this->showLocations)
				$description .= " [".$event->getLocationsList()."]";
				
			$description = str_replace(":", "\":\"", $description);
			$description = str_replace(",", "\, ", $description);
			$description = str_replace("\n", "\, ", $description);
			$description = str_ireplace("<br>", "\, ", $description);
			echo "DESCRIPTION:".$description."\r\n";
			$timenow = new DateTime('now');
			echo "DTSTAMP:" . $timenow->format('Ymd\THis') . "\r\n";
//			echo "                    ".$event->getEnd()->format('H:i:s')."                           \r\n";
			if($event->getEnd()->format('H:i:s')=="00:00:00")
			{
//				echo "X-FUNAMBOL-ALLDAY\r\n";
				echo "DTEND;TZID=Europe/London;VALUE=DATE:" . $event->getEnd()->format('Ymd') . "\r\n";
				echo "DTSTART;TZID=Europe/London;VALUE=DATE:" . $event->getStart()->format('Ymd') . "\r\n";
			}
			else 
			{
				echo "DTEND;TZID=Europe/London:" . $event->getEnd()->format('Ymd\THis') . "\r\n";
				echo "DTSTART;TZID=Europe/London:" . $event->getStart()->format('Ymd\THis') . "\r\n";
			}
		//	echo "LOCATION:" . $Qdata->valueProtected('Location') . "\r\n";
			echo "END:VEVENT\r\n";
		}
		
		echo "END:VCALENDAR";		
	}
}
